package com.yangjie.autotest.component;

import java.io.File;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.context.annotation.Scope;
import org.springframework.transaction.annotation.Transactional;

import com.yangjie.autotest.testcase.xml.Param;
import com.yangjie.autotest.testcase.xml.Params;
import com.yangjie.log.LoggerIF;

@Named("dbComp")
@Scope("prototype")
@Transactional
public class DBComponent {
	@Inject
	private LoggerIF logger = null;
	
	@Inject
	@Named("databaseComonentDAO")
	private DBComponentDao dao = null;
	
	private String tables = null;
	private String schema = null;
	private String backupDir = null;
	private String initDatasetFileDir = null;
	
	public void init(String tables, String backupDir, String initDatasetFileDir, String schema) {
		this.tables = tables;
		this.schema = schema;
		this.backupDir = backupDir;
		this.initDatasetFileDir = initDatasetFileDir;
	}
	public void execSQL(Params params) {
		for(Param param: params.getParam()) {
			execSQL(param.getValue());
		}
	}
	public void execSQL(String sql) {	
		logger.debug("exec sql:" + sql);
		dao.execSQL(sql);
	}
	
	
	public void backupTable(Params params) throws Exception {
		for(Param param: params.getParam()) {
			if(param.getName().equalsIgnoreCase("tableName")) {
				dao.backupTable(param.getValue(), backupDir, schema);
			}
		}
	}
	
	public void restoreTable() throws Exception {
		dao.restoreTable(backupDir, schema);
	}
	
	public void initTable(Params params) throws Exception {
		for(Param param: params.getParam()) {
			if(param.getName().equalsIgnoreCase("dataset")) {
				dao.initTable(initDatasetFileDir + File.separator + param.getValue(), schema);
			}
		}
	}
	
	
//	public void update(String[] args) {
//		String tableName = args[0];
//		String id = args[2];
//	}
	
}
